package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import java.math.BigDecimal;

import static com.shanxia.codec.CodeConstant.PERCENT;
import static com.shanxia.codec.Decoder.getInt32;
import static com.shanxia.codec.Decoder.getUint8;

/**
 * C -> S
 * 充电桩应答后台服务器查询 24 时电费计价策略 信息
 */
@Data
public class Cmd1102 extends ProtocolDataBase {

    public static final int MIN_SIZE = 96;

    private int startHour1;                 // 开始小时1
    private int startMinute1;               // 开始分钟1
    private int endHour1;                   // 结束小时1
    private int endMinute1;                 // 结束分钟1
    private double rate1;                   // 费率1
    private int startHour2;                 // 开始小时2
    private int startMinute2;               //开始分钟2
    private int endHour2;                   //结束小时2
    private int endMinute2;                 //结束分钟2
    private double rate2;                   //费率 2
    private int startHour3;                 //开始小时3
    private int startMinute3;               //开始分钟3
    private int endHour3;                   //结束小时3
    private int endMinute3;                 //结束分钟3
    private double rate3;                   //费率 3
    private int startHour4;
    private int startMinute4;
    private int endHour4;
    private int endMinute4;
    private double rate4;
    private int startHour5;
    private int startMinute5;
    private int endHour5;
    private int endMinute5;
    private double rate5;
    private int startHour6;
    private int startMinute6;
    private int endHour6;
    private int endMinute6;
    private double rate6;
    private int startHour7;
    private int startMinute7;
    private int endHour7;
    private int endMinute7;
    private double rate7;
    private int startHour8;
    private int startMinute8;
    private int endHour8;
    private int endMinute8;
    private double rate8;
    private int startHour9;
    private int startMinute9;
    private int endHour9;
    private int endMinute9;
    private double rate9;
    private int startHour10;
    private int startMinute10;
    private int endHour10;
    private int endMinute10;
    private double rate10;
    private int startHour11;
    private int startMinute11;
    private int endHour11;
    private int endMinute11;
    private double rate11;
    private int startHour12;
    private int startMinute12;
    private int endHour12;
    private int endMinute12;
    private double rate12;

    public Cmd1102(){
        super.setCmd(1102);
    }

    public static Cmd1102 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd1102 cmd = new Cmd1102();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < 43) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        int offset = 0;
        // 1字节开始小时
        cmd.startHour1 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute1 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour1 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute1 = getUint8(dataField[offset++]);
        // 4字节费率1
        cmd.rate1 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour2 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute2 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour2 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute2 = getUint8(dataField[offset++]);
        // 4字节费率2
        cmd.rate2 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour3 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute3 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour3 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute3 = getUint8(dataField[offset++]);
        // 4字节费率3
        cmd.rate3 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour4 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute4 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour4 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute4 = getUint8(dataField[offset++]);
        // 4字节费率4
        cmd.rate4 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour5 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute5 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour5 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute5 = getUint8(dataField[offset++]);
        // 4字节费率5
        cmd.rate5 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour6 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute6 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour6 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute6 = getUint8(dataField[offset++]);
        // 4字节费率6
        cmd.rate6 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour7 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute7 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour7 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute7 = getUint8(dataField[offset++]);
        // 4字节费率7
        cmd.rate7 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour8 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute8 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour8 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute8 = getUint8(dataField[offset++]);
        // 4字节费率8
        cmd.rate8 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour9 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute9 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour9 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute9 = getUint8(dataField[offset++]);
        // 4字节费率9
        cmd.rate9 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour10 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute10 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour10 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute10 = getUint8(dataField[offset++]);
        // 4字节费率10
        cmd.rate10 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour11 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute11 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour11 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute11 = getUint8(dataField[offset++]);
        // 4字节费率11
        cmd.rate11 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour12 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute12 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour12 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute12 = getUint8(dataField[offset++]);
        // 4字节费率12
        cmd.rate12 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        return cmd;
    }

    @Override
    public String toString() {

        StringBuffer buffer = new StringBuffer();
        buffer.append("#cmd1102#").append("\n");
        buffer.append("开始小时1:").append(startHour1).append("\n");
        buffer.append("开始分钟1:").append(startMinute1).append("\n");
        buffer.append("结束小时1:").append(endHour1).append("\n");
        buffer.append("结束分钟1:").append(endMinute1).append("\n");
        buffer.append("费率1:").append(rate1).append("\n");

        buffer.append("开始小时2:").append(startHour2).append("\n");
        buffer.append("开始分钟2:").append(startMinute2).append("\n");
        buffer.append("结束小时2:").append(endHour2).append("\n");
        buffer.append("结束分钟2:").append(endMinute2).append("\n");
        buffer.append("费率2:").append(rate2).append("\n");

        buffer.append("开始小时3:").append(startHour3).append("\n");
        buffer.append("开始分钟3:").append(startMinute3).append("\n");
        buffer.append("结束小时3:").append(endHour3).append("\n");
        buffer.append("结束分钟3:").append(endMinute3).append("\n");
        buffer.append("费率3:").append(rate3).append("\n");

        buffer.append("开始小时4:").append(startHour4).append("\n");
        buffer.append("开始分钟4:").append(startMinute4).append("\n");
        buffer.append("结束小时4:").append(endHour4).append("\n");
        buffer.append("结束分钟4:").append(endMinute4).append("\n");
        buffer.append("费率4:").append(rate4).append("\n");

        buffer.append("开始小时5:").append(startHour5).append("\n");
        buffer.append("开始分钟5:").append(startMinute5).append("\n");
        buffer.append("结束小时5:").append(endHour5).append("\n");
        buffer.append("结束分钟5:").append(endMinute5).append("\n");
        buffer.append("费率5:").append(rate5).append("\n");

        buffer.append("开始小时6:").append(startHour6).append("\n");
        buffer.append("开始分钟6:").append(startMinute6).append("\n");
        buffer.append("结束小时6:").append(endHour6).append("\n");
        buffer.append("结束分钟6:").append(endMinute6).append("\n");
        buffer.append("费率6:").append(rate6).append("\n");

        buffer.append("开始小时7:").append(startHour7).append("\n");
        buffer.append("开始分钟7:").append(startMinute7).append("\n");
        buffer.append("结束小时7:").append(endHour7).append("\n");
        buffer.append("结束分钟7:").append(endMinute7).append("\n");
        buffer.append("费率7:").append(rate7).append("\n");

        buffer.append("开始小时8:").append(startHour8).append("\n");
        buffer.append("开始分钟8:").append(startMinute8).append("\n");
        buffer.append("结束小时8:").append(endHour8).append("\n");
        buffer.append("结束分钟8:").append(endMinute8).append("\n");
        buffer.append("费率8:").append(rate8).append("\n");

        buffer.append("开始小时9:").append(startHour9).append("\n");
        buffer.append("开始分钟9:").append(startMinute9).append("\n");
        buffer.append("结束小时9:").append(endHour9).append("\n");
        buffer.append("结束分钟9:").append(endMinute9).append("\n");
        buffer.append("费率9:").append(rate9).append("\n");

        buffer.append("开始小时10:").append(startHour10).append("\n");
        buffer.append("开始分钟10:").append(startMinute10).append("\n");
        buffer.append("结束小时10:").append(endHour10).append("\n");
        buffer.append("结束分钟10:").append(endMinute10).append("\n");
        buffer.append("费率10:").append(rate1).append("\n");

        buffer.append("开始小时11:").append(startHour11).append("\n");
        buffer.append("开始分钟11:").append(startMinute11).append("\n");
        buffer.append("结束小时11:").append(endHour11).append("\n");
        buffer.append("结束分钟11:").append(endMinute11).append("\n");
        buffer.append("费率11:").append(rate11).append("\n");

        buffer.append("开始小时12:").append(startHour12).append("\n");
        buffer.append("开始分钟12:").append(startMinute12).append("\n");
        buffer.append("结束小时12:").append(endHour12).append("\n");
        buffer.append("结束分钟12:").append(endMinute12).append("\n");
        buffer.append("费率12:").append(rate12).append("\n");
        return buffer.toString();
    }
}
